module.exports = function main (){
    var xlsx = require('node-xlsx').default;
    const fs = require('fs');
    let deepClone = require("./deepcopy")
    var buffer = [
        {
            name: 'Sheet1',
            data: []
        }
    ];

//把多个excel合并一下，线虫名称现在先不去重
    var readDir = fs.readdirSync("./import");
    for (let index of readDir) {
        var excelObj = xlsx.parse(`./import/${index}`);
        // console.log(excelObj)
        var wormarrSource = deepClone(excelObj[0].data)
        var newWormarrSource = []
        for (let i = 0; i <  wormarrSource.length; i++) {
            if(wormarrSource[i][0]!==undefined){
                newWormarrSource.push(wormarrSource[i])
            }
        }
        // console.log(newWormarrSource)
        buffer[0].data.push(...newWormarrSource)
    }
// console.log( buffer[0].data)
    buffer = xlsx.build(buffer)
    fs.writeFileSync('./output/buffer.xlsx', buffer, {'flag':'w'}); // 如果文件存在，覆盖

//开始去重
    var buffer2 = [
        {
            name: '品系总和',
            data: []
        },
        {
            name: '重复品系',
            data: []
        }
    ];
    var excelObj2 = xlsx.parse('./output/buffer.xlsx');
// console.log(excelObj2)
    var wormarrSource2 = deepClone(excelObj2[0].data)
//清空原来的data，准备之后替换它
    excelObj2[0].data = []
//开始去重
    let namepool = []
    let datapool = []
    let delepool = []
    for (let i = 0; i <  wormarrSource2.length; i++) {
        if(namepool.indexOf(wormarrSource2[i][0])<0){
            namepool.push(wormarrSource2[i][0])
            datapool.push(wormarrSource2[i])
        }
    }
    for (let i = 0; i <  wormarrSource2.length; i++) {
        if(datapool.indexOf(wormarrSource2[i])<0){
            delepool.push(wormarrSource2[i])
        }
    }
// console.log(wormarrSource2.length)
// console.log(datapool.length)
// console.log(delepool)
    buffer2[0].data = datapool
    buffer2[1].data = delepool
    buffer2 = xlsx.build(buffer2)
    fs.writeFileSync('./output/result.xlsx', buffer2, {'flag':'w'}); // 如果文件存在，覆盖
    console.log("****操作成功!!!!!****")
    console.log(`共产生有效数据数据${datapool.length}条，删除重复数据${wormarrSource2.length-datapool.length}条`)
    console.log(`===========================================================================`)
    console.log(`数据excel文件在 根目录下的 output文件夹中！`)
    console.log(`==========================软件解释权归“朱旭峰”所有==============================`)
}
